package cn.senjoeson.triper.fragment;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import cn.senjoeson.mylibrary.utils.AppUtils;
import cn.senjoeson.triper.R;
import cn.senjoeson.triper.activity.SearchActivity;
import cn.senjoeson.triper.activity.SelectTripDateActivity;
import cn.senjoeson.triper.activity.TripContentActivity;
import cn.senjoeson.triper.adapter.TripRVAdapter;
import cn.senjoeson.triper.base.BaseFragment;
import cn.senjoeson.triper.base.GreenDaoManager;
import cn.senjoeson.triper.bean.Plan;
import cn.senjoeson.triper.dao.PlanDao;

/**
 * Created by senjoeson on 2017/12/29.
 */

public class TripFragment extends BaseFragment implements View.OnClickListener {


    @BindView(R.id.search_layout)
    RelativeLayout mSearchLayout;
    @BindView(R.id.et_search)
    TextView mEtSearch;
    @BindView(R.id.empty_desc)
    TextView mEmptyDesc;
    @BindView(R.id.empty)
    FrameLayout mEmpty;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerview;
    @BindView(R.id.floatbutton1)
    FloatingActionButton mFloatingActionButton;
    @BindView(R.id.root_trip_fragment)
    RelativeLayout mRelativeLayout;
    protected void initView() {
        if (checkData()) {
            mEmpty.setVisibility(View.GONE);
            mRecyclerview.setVisibility(View.VISIBLE);
        } else {
            mEmpty.setVisibility(View.VISIBLE);
            mRecyclerview.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initSetData() {
        final PlanDao planDao = GreenDaoManager.getInstance().getNewSession().getPlanDao();
        List<Plan> list = planDao.queryBuilder().build().list();
        mRecyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        TripRVAdapter tripRVAdapter = new TripRVAdapter();
        tripRVAdapter.setDatas(list);
        mRecyclerview.setAdapter(tripRVAdapter);
        tripRVAdapter.setOnItemClickListener(new TripRVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, Plan mPlan) {
                Intent intent = new Intent(AppUtils.getContext(), TripContentActivity.class);
                long planId = mPlan.getPlanId();
                intent.putExtra("planId", planId);
                startActivity(intent);
            }
        });

    }

    private boolean checkData() {
        PlanDao planDao = GreenDaoManager.getInstance().getMaster().newSession().getPlanDao();
        List<Plan> list = planDao.queryBuilder().build().list();
        return list != null && list.size() > 0;
    }

    @Override
    protected int setContentResId() {
        if (checkData()) {
            return R.layout.fragment_trip_empty;
        } else {
            return R.layout.fragment_trip;
        }
    }


    @Override
    protected void initListener() {
        mSearchLayout.setOnClickListener(this);
        mFloatingActionButton.setOnClickListener(this);
        mEtSearch.setOnClickListener(this);
        mRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRelativeLayout.requestFocus();
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_layout:
            case R.id.et_search:
                startActivity(SearchActivity.class);
                break;

            case R.id.floatbutton1:
                startActivity(SelectTripDateActivity.class);
                break;
        }
    }







}
